//
//  CellForPostModel.m
//  Qyer
//
//  Created by dllo on 15/10/20.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "CellForPostModel.h"

@implementation CellForPostModel

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = DAY_BACKGROUND;
        self.title = [[UILabel alloc] init];
        self.title.font = [UIFont systemFontOfSize:18];
        self.title.numberOfLines = 2;
        [self.contentView addSubview:self.title];
        
        self.name = [[UILabel alloc] init];
        self.name.textColor = [UIColor lightGrayColor];
        self.name.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.name];
        
        self.time = [[UILabel alloc] init];
        self.time.textColor = [UIColor lightGrayColor];
        self.time.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.time];
        
        self.top = [[UILabel alloc] init];
        self.top.textAlignment = NSTextAlignmentCenter;
        self.top.textColor = WHITE_COLOR;
        self.top.backgroundColor = UIColorWithRGBA(42, 206, 227, 1.0);
        self.top.text = @"置顶";
        self.top.layer.cornerRadius = 3;
        [self.contentView addSubview:self.top];
        
        self.essence = [[UILabel alloc] init];
        self.essence.textAlignment = NSTextAlignmentCenter;
        self.essence.textColor = WHITE_COLOR;
        self.essence.backgroundColor = UIColorWithRGBA(62, 201, 179, 1.0);
        self.essence.layer.cornerRadius = 3;
        self.essence.text = @"精华";
        [self.contentView addSubview:self.essence];
        
        self.replyCount = [[UILabel alloc] init];
        self.replyCount.font = [UIFont systemFontOfSize:15.];
        self.replyCount.textColor = UIColorWithRGBA(56, 201, 151, 1.0);
        self.replyCount.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.replyCount];
        
        self.replyImage = [[UIImageView alloc] init];
        [self.replyImage setImage:[UIImage imageNamed:@"iconfont-huifu"]];
        [self.contentView addSubview:self.replyImage];
        
        self.pickCount = [[UILabel alloc] init];
        self.pickCount.textColor = [UIColor lightGrayColor];
        self.pickCount.font = [UIFont systemFontOfSize:15.];
        self.pickCount.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.pickCount];
        
        self.pickImage = [[UIImageView alloc] init];
        [self.pickImage setImage:[UIImage imageNamed:@"iconfont-view"]];
        [self.contentView addSubview:self.pickImage];
        
        self.date = [NSDate date];
        //self.attributedStr = [[NSMutableAttributedString alloc] init];
    }
    return self;
}

- (void)setModel:(BaseModel *)model {
    //PostModel *postModel = (PostModel *)model;
    
}
- (void)setData:(PostModel *)postModel text:(NSString *)seatchText {
    self.attributedStr = [self illuminatedString:postModel.title rangeStr:seatchText];
    self.height = [StringWithHeight HeightWithText:postModel.title width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:18.]];
    _title.frame = CRM(20 * WIDTH, 10 * HEIGHT, SCREEN_WIDTH - 40 * WIDTH, self.height);
    _title.attributedText = self.attributedStr;
    //_title.text = postModel.title;

    _top.frame = CRM(20 * WIDTH, self.height + 15, 40 * WIDTH, 25);
    _essence.frame = CRM(SCREEN_WIDTH - 60 * WIDTH, _top.frame.origin.y, 40 * WIDTH, 25);
    CGFloat width = [StringWithWidth widthWithText:postModel.username height:25. font:[UIFont systemFontOfSize:15.]];
    _name.frame = CRM(20 * WIDTH, self.height + 45, width, 25);
    _name.text = postModel.username;
    
    _time.frame = CRM(_name.frame.size.width + 25 * WIDTH, _name.frame.origin.y, 100 * WIDTH, 25);
    
    int i = _date.timeIntervalSince1970 - postModel.lastpost;
    if (i < 60) {
        _time.text = @"刚刚";
    } else if (i < 60 * 60) {
        _time.text = [NSString stringWithFormat:@"%d分钟前", i / 60];
    } else if (i < 24 * 60 * 60) {
        _time.text = [NSString stringWithFormat:@"%d小时前", i / 60 / 60];
    } else if (i < 24 * 60 * 60 * 7) {
        _time.text = [NSString stringWithFormat:@"%d天前", i / 60 / 60 / 24];
    } else {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:postModel.lastpost];
        NSString *str = [formatter stringFromDate:date];
        _time.text = str;
    }
    if (postModel.is_top == YES) {
        self.top.hidden = NO;
    } else {
        self.top.hidden = YES;
    }
    if (postModel.digest_level != 0) {
        self.essence.hidden = NO;
    } else {
        self.essence.hidden = YES;
    }
    
    width = [StringWithWidth widthWithText:postModel.replys height:25 font:[UIFont systemFontOfSize:15.]];
    _replyCount.frame = CRM(SCREEN_WIDTH - 20 * WIDTH - width, _name.frame.origin.y, width, 25);
    _replyCount.text = postModel.replys;
    
    _replyImage.frame = CRM(_replyCount.frame.origin.x - 20 * WIDTH, _name.frame.origin.y + 7 * HEIGHT, 18, 16);
    
    width = [StringWithWidth widthWithText:postModel.views height:25 font:[UIFont systemFontOfSize:15]];
    
    _pickCount.frame = CRM(_replyImage.frame.origin.x - 20 * WIDTH - width, _name.frame.origin.y, width, 25);
    _pickCount.text = postModel.views;
    
    _pickImage.frame = CRM(_pickCount.frame.origin.x - 20 * WIDTH, _name.frame.origin.y + 5.5 * HEIGHT, 18,  16);
    
    
    
}
- (NSAttributedString *)illuminatedString:(NSString *)text rangeStr:(NSString *)str {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = [text rangeOfString:str];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    
    return [attributedStr copy];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
